package cn.edu.zafu.bigdata.fui;
/*使用HashMap实现学生成绩管理
描述：编写一个简单的学生成绩管理系统，使用HashMap存储每个学生的成绩。
要求：
1.创建一个StudentGradeManager类，其中包含一个HashMap<String, Integer>来存储学生姓名与其对应的成绩。
2.提供以下方法：
1）void addStudent(String name, int grade)：添加学生成绩。
2）int getGrade(String name)：根据学生姓名获取成绩，若学生不存在则返回-1。
3）void removeStudent(String name)：删除学生成绩。
3.编写测试类，添加几个学生成绩并进行查询和删除操作。
* */

import java.util.HashMap;

public class TestHashMap {
    static class StudentGradeManager{
        final private HashMap<String, Integer> studentGrades;
        StudentGradeManager(){
            studentGrades = new HashMap<String, Integer>();
        }
        private boolean findStudent(String name){
            return studentGrades.containsKey(name);
        }
        public void addStudent(String name, int grade){
            if (findStudent(name)) {
                throw new IllegalArgumentException("studentExist");
            };
            studentGrades.put(name, grade);
            System.out.println("Successfully add student" + name + ":" + grade);
        }
        public int getGrade(String name){
            if (!findStudent(name)) {
                throw new IllegalArgumentException("studentNotExist");
            };
            return studentGrades.get(name);
        }
        public void removeStudent(String name){
            if (!findStudent(name)) {
                throw new IllegalArgumentException("studentNotExist");
            };
            studentGrades.remove(name);
            System.out.println("Successfully removed student: " + name);
        }
    }

    public static void main(String[] args) {
        String name = "testName";
        int grade = 100;
        StudentGradeManager studentGradeManager = new StudentGradeManager();
        studentGradeManager.addStudent(name, grade);
        System.out.println("getGrade: " + studentGradeManager.getGrade(name));
        studentGradeManager.removeStudent(name);
    }
}
